package com.pengbei.enshrine.common.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.pengbei.enshrine.R;
import com.pengbei.enshrine.common.dialog.callback.PermissionIntroduceAdapter;
import com.pengbei.enshrine.common.dialog.callback.PermissionIntroduceCallBack;
import com.pengbei.enshrine.network.bean.PermissionIntroduceModel;

import java.util.List;

public class PermissionIntroduceDialog {
    private Activity activity;
    private Dialog dialog;
    private RelativeLayout rl_rdp;
    private RecyclerView rv_lp;
    private TextView tv_lp_sure;
    private TextView tv_lp_cancal;
    private Display display;
    private PermissionIntroduceAdapter adapter;

    public PermissionIntroduceDialog(Activity activity) {
        this.activity = activity;
        WindowManager windowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }
    public PermissionIntroduceDialog builder() {
        View view = LayoutInflater.from(activity).inflate(R.layout.dialog_permissionintroduce, null);
        rl_rdp = (RelativeLayout) view.findViewById(R.id.rl_rdp);
        rv_lp = (RecyclerView) view.findViewById(R.id.rv_lp);
        tv_lp_sure = (TextView) view.findViewById(R.id.tv_lp_sure);
        tv_lp_cancal = (TextView) view.findViewById(R.id.tv_lp_cancal);

        LinearLayoutManager layoutManager = new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false);
        //初始化recyclerView
        rv_lp.setLayoutManager(layoutManager);
//        rv_lp.addItemDecoration(new DividerItemDecoration(activity,LinearLayout.VERTICAL));//子View的化妆间
        rv_lp.setItemAnimator(new DefaultItemAnimator());

        dialog = new Dialog(activity, R.style.AlertDialogStyle);
        dialog.getWindow().setWindowAnimations(R.style.dialog_animation2);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(view);

        rl_rdp.setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth()), LinearLayout.LayoutParams.WRAP_CONTENT));

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(lp);

        return this;
    }

    public PermissionIntroduceDialog show(List<PermissionIntroduceModel> list, final PermissionIntroduceCallBack listPermissionCallBack) {

        //初始化adapter
        adapter = new PermissionIntroduceAdapter(activity, list);
        rv_lp.setAdapter(adapter);

        tv_lp_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                listPermissionCallBack.Sure();

            }
        });

        tv_lp_cancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                listPermissionCallBack.Cancal();

            }
        });

        rl_rdp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                listPermissionCallBack.Cancal();

            }
        });

        return this;

    }

    public void show() {
        dialog.show();
    }

    public boolean isShowing(){
        if(dialog != null) {
            return dialog.isShowing();
        } else {
            return false;
        }
    }
}